package util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;

public class CheckBean {
	
	/**
	 * 判断Bean的属性是否是空字符串，如果是，则转换为null
	 * 
	 * @param object
	 */
	public static <T> void isBeanPropertyNotEmpty(Object object) {
		Class<? extends Object> clazz = object.getClass();
		Field[] fields = clazz.getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			Field field = fields[i];
			String fieldName = field.getName();
			Class<?> fieldType = field.getType();
			try {
				
				System.out.println("fieldName : " + fieldName + "--fieldType : " + fieldType);
				System.out.println("fieldValue : " + BeanUtils.getProperty(object, fieldName));
				
				if (fieldType.equals(String.class)) {

					String val = BeanUtils.getProperty(object, fieldName);
					if (val != null && "".equals(val)) {
						BeanUtils.setProperty(object, fieldName, null);
						System.out.println("copyfield : " + BeanUtils.getProperty(object, fieldName));
					}
				}

			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
		}
	}
}
